<?php
class HomeAction extends Action{
	
	public function _initialize() {
		global $myschool;
		 $myschool = M('user_basicfile')->field('schoolid,school')->where('uid='.$this->mid)->find();
		 
	}
	
	public function index() {
		global $myschool;
		
		$db_prefix  =  C('DB_PREFIX');
		if($_POST['k']){
	    $search_key   =  $this->__getSearchKey ();
	    $map = "g.title LIKE '%{$search_key}%' AND g.school='{$myschool[school]}'";
		$cmap = "title LIKE '%{$search_key}%' AND school='{$myschool[school]}'";
		}else{
		$map="g.school='{$myschool[school]}'";
		$cmap="school='{$myschool[school]}'";
		}

		$count = M('goods')->where($cmap)->count();    //计算总数
		$p = new Page ( $count, 15 );
		$goodslist = M('')->field('g.id, g.uid, g.uname, g.title, g.gprice, g.gnumber, g.gimage, g.utime, bf.qq')
		->table("{$db_prefix}goods AS g LEFT JOIN {$db_prefix}user_basicfile AS bf ON g.uid=bf.uid")
		->where($map)->limit($p->firstRow.','.$p->listRows)->order('g.utime DESC')->findAll ();
		$page = $p->show ();
		//print_r($goodslist);
		//echo $count;
		$this->assign ( "page", $page );
		$this->assign('goodslist',$goodslist);
		$this->assign ( 'myschool', $myschool );
		$this->setTitle('首页出售信息');
		$this->display();
	}
	public function  qiugou(){
		global $myschool;
		//echo $myschool['school'];
		$db_prefix  =  C('DB_PREFIX');
		if($_POST['k']){
	    $search_key   =  $this->__getSearchKey ();
	    $map = "g.title LIKE '%{$search_key}%' AND g.school='{$myschool[school]}'";
		$cmap = "title LIKE '%{$search_key}%' AND school='{$myschool[school]}'";
		}else{
		$map="g.school='{$myschool[school]}'";
		$cmap="school='{$myschool[school]}'";
		}
	
		$count = M('qiugou')->where($cmap)->count();    //计算总数
		$p = new Page ( $count, 15 );
		$goodslist = M( '' )->field('g.id, g.uid, g.uname, g.title, g.gprice, g.gnumber, g.utime, bf.qq')
		->table("{$db_prefix}qiugou AS g LEFT JOIN {$db_prefix}user_basicfile AS bf ON g.uid=bf.uid")
		->where($map)->limit($p->firstRow.','.$p->listRows)->order('g.utime DESC')->findAll ();
		$page = $p->show ();
		$this->assign ( "page", $page );
		//print_r($goodslist);
		$this->assign('goodslist',$goodslist);
		$this->assign ( 'myschool', $myschool );
		$this->setTitle('首页求购信息');
		$this->display();
	
    }
    public function boy(){
        global $myschool;
        //print_r($myschool);
        
        $user = M('user_basicfile');
        $count= $user->where("`schoolid`={$myschool['schoolid']} and sex=1")->count();
        $p = new Page ( $count, 16 );
        $list=$user->where("`schoolid`={$myschool['schoolid']} and sex=1")->limit($p->firstRow.','.$p->listRows)->order('uid DESC')->findAll();
        
        $page = $p->show ();
        $this->assign('list',$list);
        $this->assign ( "page", $page );
        $this->display('user');
    }
    public function girl(){
        global $myschool;
        $user = M('user_basicfile');
        $count= $user->where("`schoolid`={$myschool['schoolid']} and sex=0")->count();
        $p = new Page ( $count, 16 );
        $list=$user->where("`schoolid`={$myschool['schoolid']} and sex=0")->limit($p->firstRow.','.$p->listRows)->order('uid DESC')->findAll();
        //print_r($list);
        $page = $p->show ();
        $this->assign('list',$list);
        $this->assign ( "page", $page );
        $this->display('user');
    }
	private function __getSearchKey() {
		$key = '';
		// 为使搜索条件在分页时也有效，将搜索条件记录到SESSION中
		if (isset ( $_REQUEST ['k'] ) && ! empty ( $_REQUEST ['k'] )) {
			if ($_GET ['k']) {
				$key = html_entity_decode ( urldecode ( $_GET ['k'] ), ENT_QUOTES );
			} elseif ($_POST ['k']) {
				$key = $_POST ['k'];
			}
			// 关键字不能超过30个字符
			if (mb_strlen ( $key, 'UTF8' ) > 30)
				$key = mb_substr ( $key, 0, 30, 'UTF8' );
			$_SESSION ['home_user_search_key'] = serialize ( $key );
		
		} else if (is_numeric ( $_GET [C ( 'VAR_PAGE' )] )) {
			$key = unserialize ( $_SESSION ['home_user_search_key'] );
		
		} else {
			//unset($_SESSION['home_user_search_key']);
		}
		//echo unserialize ($_SESSION ['home_user_search_key']);
		return trim ( $key );
	}
}	
?>
